<?php

namespace IDock\Insurance\AICS\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\AICS\AICSEnums;
use IDock\Insurance\AICS\Response\AllAICSResponse;
use IDock\Printer\VehicleModelPrinter;
use IDock\Printer\VehiclePrinter;

class VehicleModelResponse extends AllAICSResponse
{

    public function resolve(): Response
    {
        $body = $this->getBody();
        $body = str_replace('&lt;','<',$body);
        $body = str_replace('&gt;','>',$body);
        $body = str_replace("'",'"',$body);
        $start = strpos($body,'<response>');
        $end = strpos($body,'</response>');
        $body = substr($body,$start + 10,intval($end - $start -10));
        if ($body) {
            libxml_disable_entity_loader(false);

            $body = simplexml_load_string($body);
            $body = $this->objectToArray($body);
            $errorCode = $body['returnCode'] ?? '';
            $message = $body['msg'] ?? '未知错误';
            $this->setBody($body);
            if ($errorCode !== 'E0000') {
                $this->setError($message);
                return $this;
            }
        }

        return $this;
    }

    public function printerInit(): Response
    {
        $data = $this->getBody();
        $modelList = $data['vehicleInfo'] ?? [];

        $vehicleModelPrinter = new VehicleModelPrinter();
        $this->printer = $vehicleModelPrinter;
        if(!isset($modelList[0])){
            $tmp = $modelList;
            unset($modelList);
            $modelList[] = $tmp;
        }
        foreach ($modelList as $datum) {
            $vehiclePrinter = new VehiclePrinter();
            $vehiclePrinter->vehicleModelCode = $datum['vehicleId'] ?? '';
            $vehiclePrinter->vehicleName = $datum['vehicleName'] ?? '';
            $vehiclePrinter->brandName = $datum['vehicleBrand'] ?? '';
            $vehiclePrinter->vehicleExhaust = $datum['vehicleDisplacement'] ?? '';
            $vehicleType = $datum['vehicleType'] ?: '';

            $vehiclePrinter->vehicleImport = array_search($vehicleType,IDockEnums::$importFlag);
            $vehiclePrinter->vehicleImportName = $vehicleType;

            $vehiclePrinter->vehiclePrice = $datum['vehiclePrice'] ?? '';
            $vehiclePrinter->vehiclePriceWithOutTax = $datum['vehiclePrice'] ?? '';
            $vehiclePrinter->vehicleSeat = $datum['vehicleSeat'] ?? '';
            $vehiclePrinter->remark = $datum['vehicleRemark'] ?? '';

            $vehiclePrinter->familyName = $datum['vehicleSeries'] ?? '';
            $vehiclePrinter->power = $datum['vehiclePower'] ?? '';
            $vehiclePrinter->vehicleYear = $datum['vehicleYear'] ?? '';
            $vehiclePrinter->vehicleTonnage = $datum['vehicleTonnage'] ?? '';
            $vehiclePrinter->wholeWeight = round(($datum['curbWeightMin'] ?? 0) * 1000 ,2) ;
            $vehiclePrinter->isNewEnergy = 0;
            $vehiclePrinter->energyTypeCode = 'E01';

            $vehicleModelPrinter->setModelList($vehiclePrinter);
        }

        $this->printer = $vehicleModelPrinter;

        return $this;
    }
}
